package service;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.Exam_info_import;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;


/**
 * Servlet implementation class infoservlet
 */
@WebServlet("/infoservlet")
public class InfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();
        org.json.JSONObject jsonObject=new org.json.JSONObject();
        try {
			jsonObject.put("code",0);
		} catch (org.json.JSONException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
        try {
			jsonObject.put("msg","");
		} catch (org.json.JSONException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
        try {
			jsonObject.put("count",0);
		} catch (org.json.JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
        String sql="select status,period,college,course,classes,date,examTime,location,teacher,teacherRequire from exam_info";
        JSONArray result=null;
        try{
            Exam_info_import eii=new Exam_info_import();
            try {
				result = JSONArray.fromObject( eii.examinfolist(sql));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }catch(net.sf.json.JSONException ey)
        {
            ey.printStackTrace();
        }
        try {
			jsonObject.put("data",result);
		} catch (org.json.JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        System.out.println(jsonObject.toString());
        out.println(jsonObject.toString());
        out.flush();
        out.close();
	}

}